﻿// **************************************************************************
//    Class Created by Mick Doherty (Dotnetrix) March 2010
//    http://dotnetrix.co.uk/
//
//    ...for GlassUI (an AeroGlass UI Library)
//    http://glassui.codeplex.com/
// **************************************************************************

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace GlassUI
{
    [ToolboxBitmap(typeof(TabControl))]
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip)]
    public class ToolStripTab : ToolStripItem
    {
        private bool isChecked;
        private TabAlignment alignment;

        [DefaultValue(false)]
        public bool Checked
        {
            get { return isChecked; }
            set 
            {
                if (isChecked != value)
                {
                    isChecked = value;
                    OnCheckedChanged(EventArgs.Empty);
                }
            }
        }

        [DefaultValue(typeof(TabAlignment),"Top")]
        public TabAlignment TabOrientation
        {
            get 
            { 
                return alignment; 
            }
            set
            {
                alignment = value;
                this.Invalidate();
            }
        }

        protected override void OnClick(EventArgs e)
        {
            if (!this.Checked)
            {
                base.OnClick(e);
                this.Checked = true;
            }
        }

        protected virtual void OnCheckedChanged(EventArgs e)
        {
            ToolStrip toolstrip = this.Owner;
            if (toolstrip != null)
            foreach (ToolStripItem item in toolstrip.Items)
            {
                ToolStripTab tabItem = item as ToolStripTab;
                if (tabItem != null && item != this && this.Checked)
                    DeselectItem(tabItem);
            }
            this.Invalidate();
        }

        private static void DeselectItem(ToolStripTab item)
        {
            if (item != null)
                item.Checked = false;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            ToolStripRenderer renderer = this.Owner.Renderer;
            if (renderer != null)
            {
                ToolStripItemRenderEventArgs ev = new ToolStripItemRenderEventArgs(e.Graphics, this);
                renderer.DrawItemBackground(ev);
                Rectangle bounds = new Rectangle(Point.Empty, this.Size);
                bounds.Inflate(-3, -3);
                GlassUtilities.RenderStandardContent(e.Graphics, bounds, this.Text, this.Font, this.Image, this.TextAlign, this.ImageAlign, this.TextImageRelation, this.ForeColor, false, 8);
            }
            
        }

        public override Size GetPreferredSize(Size proposedSize)
        {
            // return base.GetPreferredSize(proposedSize);
            Size imageSize = Size.Empty;
            Size availableTextSize = Size.Empty;
            Size textSize = Size.Empty;

            Rectangle contentBounds = Rectangle.Empty;
            TextFormatFlags format = TextFormatFlags.TextBoxControl;

            Image img = this.Image;
            if (img != null)
                imageSize = img.Size;

            switch (this.TextImageRelation)
            {
                case TextImageRelation.ImageAboveText:
                case TextImageRelation.TextAboveImage:
                    availableTextSize = new Size(proposedSize.Width, proposedSize.Height - imageSize.Height);
                    break;

                case TextImageRelation.ImageBeforeText:
                case TextImageRelation.TextBeforeImage:
                    availableTextSize = new Size(proposedSize.Width - imageSize.Width, proposedSize.Height);
                    break;

                default:
                    availableTextSize = proposedSize;
                    break;
            }

            if (this.Text.Length > 0)
            {
                using (Graphics graphics = this.Owner.CreateGraphics())
                {
                    textSize = GlassUtilities.MeasureThemedText(graphics, this.Text, this.Font, availableTextSize, format, 8);
                }
            }

            switch (this.TextImageRelation)
            {
                case TextImageRelation.ImageAboveText:
                case TextImageRelation.TextAboveImage:
                    contentBounds.Width = Math.Max(imageSize.Width, textSize.Width);
                    contentBounds.Height = imageSize.Height + textSize.Height;
                    break;

                case TextImageRelation.ImageBeforeText:
                case TextImageRelation.TextBeforeImage:
                    contentBounds.Width = imageSize.Width + textSize.Width;
                    contentBounds.Height = Math.Max(imageSize.Height, textSize.Height);
                    break;

                default:
                    contentBounds.Width = Math.Max(imageSize.Width, textSize.Width);
                    contentBounds.Height = Math.Max(imageSize.Height, textSize.Height);
                    break;
            }

            contentBounds.Width += Padding.Horizontal + 8;
            contentBounds.Height += Padding.Vertical + 8;

            return contentBounds.Size;
        }

    }
}
